using System.Windows;
using System.Xml;

namespace OfficeOpenXml
{
    public class ColorScale : RuleVisualizer
    {
        private XmlNode _percentileNode;
        private XmlNode _startColorNode;
        private XmlNode _midpointColorNode;
        private XmlNode _endColorNode;

        public ColorScale(ConditionalFormattingRule conditionalFormatting) : base(conditionalFormatting)
        {
            _xmlNode = ConditionalFormattingRule.ConditionalFormatting.Worksheet.WorksheetXml.CreateElement("dataBar",
                                                                                                            ExcelPackage
                                                                                                                .
                                                                                                                schemaMain);
            ConditionalFormattingRule.XmlNode.AppendChild(this._xmlNode);
            Min = 0;
            Max = 0;
            StartColor = "FFFF0000";
            MidpointColor ="FFFFFF00";
            EndColor = "FF00B050";

        }

        public ColorScale(ConditionalFormattingRule conditionalFormatting, XmlNode ruleNode) : base(conditionalFormatting, ruleNode)
        {
        }

        /// <summary>
        /// Defines middle point (default 50 percent)
        /// </summary>
        public int Percentile
        {
            get
            {               
                if (_percentileNode == null)
                {
                    _percentileNode = this.GetPercentileNode();
                }
                return int.Parse(_percentileNode.Attributes["val"].Value);
            }
            set
            {
                if (_percentileNode == null)
                {
                    _percentileNode = this.GetPercentileNode();
                }
                _percentileNode.Attributes["val"].Value = value.ToString();
            }
        }

        public string StartColor
        {
            get
            {
                if (_startColorNode == null)
                {
                    _startColorNode = GetStartColorNode();
                }                
                return _startColorNode.Attributes["rgb"].Value;
            }
            set
            {
                if (_startColorNode == null)
                {
                    _startColorNode = GetStartColorNode();
                }
                _startColorNode.Attributes["rgb"].Value = value;
            }
        }

        public string MidpointColor
        {
            get
            {
                if (_midpointColorNode == null)
                {
                    _midpointColorNode = GetMidpoitColorNode();
                }
                return _midpointColorNode.Attributes["rgb"].Value;
            }
            set
            {
                if (_midpointColorNode == null)
                {
                    _midpointColorNode = GetMidpoitColorNode();
                }
                _midpointColorNode.Attributes["rgb"].Value = value;
            }
        }


        public string EndColor
        {
            get
            {
                if (_endColorNode == null)
                {
                    _endColorNode = GetEndColorNode();
                }
                return _endColorNode.Attributes["rgb"].Value;
            }
            set
            {
                if(_endColorNode == null)
                {
                    _endColorNode = GetEndColorNode();
                }
                _endColorNode.Attributes["rgb"].Value = value;
            }
        }

        private XmlNode GetEndColorNode()
        {
            XmlNode node = null;
            int num = 0;
            foreach (XmlNode childNode in _xmlNode.ChildNodes)
            {
                if (childNode.Name == "color")
                {
                    if (num == 2)
                    {
                        node = childNode;
                        break;
                    }
                    num++;
                }
            }
            if (node == null)
            {
                XmlElement element = this.ConditionalFormattingRule.ConditionalFormatting.Worksheet.WorksheetXml.CreateElement("color", ExcelPackage.schemaMain);
                element.SetAttribute("rbg", "FF00B050");
                node = element;
            }
            return node;
        }

        private XmlNode GetMidpoitColorNode()
        {
            XmlNode node = null;
            int num = 0;
            foreach (XmlNode childNode in _xmlNode.ChildNodes)
            {
                if (childNode.Name == "color")
                {
                    if (num == 1)
                    {
                        node = childNode;
                        break;
                    }
                    num++;
                }
            }
            if (node == null)
            {
                XmlElement element = this.ConditionalFormattingRule.ConditionalFormatting.Worksheet.WorksheetXml.CreateElement("color", ExcelPackage.schemaMain);
                element.SetAttribute("rbg", "FFFFFF00");
                node = element;
            }
            return node;
        }

        private XmlNode GetStartColorNode()
        {
            XmlNode node = null;
            foreach (XmlNode childNode in _xmlNode.ChildNodes)
            {
                if (childNode.Name == "color")
                {
                    node = childNode;
                    break;
                }
            }

            if (node == null)
            {
                XmlElement element = this.ConditionalFormattingRule.ConditionalFormatting.Worksheet.WorksheetXml.CreateElement("color", ExcelPackage.schemaMain);
                element.SetAttribute("rbg", "FFFF0000");
                _xmlNode.AppendChild(element);
                node = element;
            }
            return node;
        }


        private XmlNode GetPercentileNode()
        {
            XmlNode node = null;
            foreach (XmlNode childNode in _xmlNode.ChildNodes)
            {
                if (childNode.Name == "cfvo" && childNode.Attributes["type"].Value == "percentile")
                {
                    node = childNode;
                    break;
                }
            }

            if (node == null)
            {
                XmlElement element =
                    this.ConditionalFormattingRule.ConditionalFormatting.Worksheet.WorksheetXml.CreateElement("cfvo",
                                                                                                              ExcelPackage
                                                                                                                  .
                                                                                                                  schemaMain);
                element.SetAttribute("type", "percentile");
                element.SetAttribute("val", "50");
                _xmlNode.AppendChild(element);
                node = element;
            }
            return node;
        }
    }
}